package client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author casshern
 */
public class JoinGameFrame extends JFrame implements ActionListener {

    private JTextField textField;
    private JButton join;
    private JLabel label;
    private Client client;

    public JoinGameFrame() {
        textField = new JTextField("localhost");
        textField.setPreferredSize(new Dimension(100, 20));
        join = new JButton("Join");
        join.setPreferredSize(new Dimension(100, 80));
        join.addActionListener(this);
        label = new JLabel("Enter the Address and Click Join :");
        this.add(label, BorderLayout.NORTH);
        this.add(textField, BorderLayout.CENTER);
        this.setLayout(new FlowLayout());
        this.add(join);
        this.setLocation(400, 200);
        this.setSize(500, 150);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public void setStatus() {
        textField.setEditable(false);
        label.setText("Waiting the Server starts the Game");
        join.setEnabled(false);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == join) {
            try {
                client = new Client(textField.getText(), this);
                client.setID(2);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "Connecting to server failed.");
                ex.printStackTrace();
                System.exit(1);
            }
        }
    }
}
